package com.horatio.ai;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.horatio.ai.struts.AiConfEntity;
import com.horatio.db.DBDataSourceFactory;

/**
 * AI管理器
 * @author hongxiao.z
 * @date   2014-5-16  上午11:38:57
 */
public class AIManager 
{
	private AIManager(){}
	
	private static volatile AIManager instance;
	
	private static Logger log = LoggerFactory.getLogger(AIManager.class);
	
	/**
	 * 加载AI数据
	 * @create	hongxiao.z      2014-5-16 上午11:42:23
	 */
	public static void loadAi()
	{
		AIManager manager = new AIManager();
		
		//TODO 从DB加载所有AI设置
		List<AiConfEntity> models = loadDB();
		
		for (AiConfEntity aiStr : models)
		{
			analysis(manager, aiStr);
		}
		
		instance = manager;
	}
	
	private static String sql = "select name, AWAIT, PREPARE_FIGHT, FIGHT, BREAK_FIGHT from ai_config";
	
	/**
	 * 从DB获取数据列表
	 * @return
	 * @create	hongxiao.z      2014-5-16 下午7:12:21
	 */
	private static List<AiConfEntity> loadDB()
	{
		log.info(" >>>>>>>>>>>>>>>>>>>> 加载AI配置开始......");
		
		List<AiConfEntity> models = new ArrayList<>();
		Connection conn = DBDataSourceFactory.getConnection(DBDataSourceFactory.AI_KEY);
		
        PreparedStatement ps = null;
		try 
		{
			ps = conn.prepareStatement(sql);
			ResultSet s = ps.executeQuery(sql);
			while(s.next())
			{
				AiConfEntity conf = new AiConfEntity();
				conf.setName(s.getString(1));
				conf.setAWAIT(s.getString(2));
				conf.setPREPARE_FIGHT(s.getString(3));
				conf.setFIGHT(s.getString(4));
				conf.setBREAK_FIGHT(s.getString(5));
				
				conf.analysis();
				
				models.add(conf);
				
				log.info("加载AI配置 --> " + conf.getName());
				
//				log.info("加载AI配置 --> " + conf.getName() + 
//						"<AWAIT:" + conf.getAWAIT() + ">-<PREPARE_FIGHT:" + conf.getPREPARE_FIGHT() + 
//						">-<FIGHT:" + conf.getFIGHT() + ">-<BREAK_FIGHT:" + conf.getBREAK_FIGHT() + ">");
			}
		} 
		catch (SQLException e1) 
		{
			e1.printStackTrace();
		}
		finally
		{
			try {
				if(ps != null) ps.close();
				conn.close();
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}	
		}
		
		log.info(" >>>>>>>>>>>>>>>>>>>> 加载AI配置完毕, AI配置数量[" + models.size() + "]!");
		
		return models;
	}
	
	/**
	 * 解析
	 * @param model
	 * @create	hongxiao.z      2014-5-16 下午3:51:59
	 */
	private static void analysis(AIManager manager, AiConfEntity model)
	{
		manager.all.put(model.getName(), model);
	}
	
	/**
	 * 获取管理器的实例
	 * @return
	 * @create	hongxiao.z      2014-5-16 下午3:55:23
	 */
	public static AIManager getInstance()
	{
		return instance;
	}
	
	/**
	 * 所有的AI集合
	 */
	private Map<String, Object> all = new HashMap<>();
	
	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, cal.get(Calendar.DATE) - 3);
		
		System.out.println(cal.getTime());
		System.out.println(cal.getTimeInMillis());
		
//		loadAi();
//		
//		List<String> strs = new ArrayList<>();
//		strs.add("1:1");
//		strs.add("2:2");
//		
//		String str = JSONObject.toJSONString(strs);
//		System.out.println(str);
//		
//		List<String> st = JSONArray.parseObject(str, ArrayList.class);
//		st.add("3:3");
//		System.out.println(st);
	}
}
